<?php
//获取请求必传参
$otherNodeRoleParams = $route->bodyParams['other_node_role'];

//待处理的总数
$processedCount = count($otherNodeRoleParams);

//已完成数
$successCount = 0;

//循环处理每组更新信息
for($i=0;$i<count($otherNodeRoleParams);$i++){

    $tempNodeId  = $otherNodeRoleParams[$i]['osid'];
    $tempRoleArr = $otherNodeRoleParams[$i]['node_role'];

    //首先需要查询该节点现有的角色信息
    $searchRoleSearchSql = "SELECT orm_orid FROM sys_organizational_role_middle WHERE orm_osid =  $tempNodeId";
    
    //获取当前查询节点所拥有的角色ID
    $roleData = $db->mysqlDB->query($searchRoleSearchSql);

    //将需要写入的权限和现有权限进行组合去重
    $newRoleData = unique_2d_array_by_key(array_merge($roleData,$tempRoleArr),"orm_orid");

    //关闭事物自动提交
    $db->mysqlDB->falseAutoCommit();

    //判断是否有需要更新的角色，如没有则直接进行下次的循环
    if(count($newRoleData)<=0){
       $successCount++;
       continue;  
    }

    //删除原有权限
    $deleteParams = array(
        "delete_where"       => "orm_osid",
        "orm_osid"           => $tempNodeId,
        "isPhysicallyDelete" => "1"
    );

    //删除原有角色
    $R1 = $db->mysqlDB->automaticDelete($deleteParams)->delete("sys_organizational_role_middle");

    //拼接多新增语句SQL
    $addSql = "INSERT INTO sys_organizational_role_middle";

    $keys = "(orm_osid,orm_orid) VALUES";

    $values = "";

    foreach($newRoleData as $v){

        $rid = $v['orm_orid'];

        $tmpValue = "(";
        $tmpValue .= $tempNodeId . ",'" . $rid . "'";
        $tmpValue .= "),";

        $values .= $tmpValue . "";

    }

    //拼接最终SQL
    $addSql .= $keys .= rtrim($values, ",");


    //执行SQL
    $R2 = $db->mysqlDB->queryTransaction($addSql);

    //判断两个操作是否执行成功
    if ($R1 || !$R2) {
        $db->mysqlDB->rollBackCommint();
    }else{
        $db->mysqlDB->actionCommint();
        $db->mysqlDB->trueAutoCommit();
        $successCount++;
    }
}

//判断完成总数
if($successCount == $processedCount){
   $response->responseData(true);
}else{
   $response->responseData(false);
}